package com.novoda.runbuddy.provider;

import android.content.ContentProvider;
import android.content.UriMatcher;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class RunBuddyContentProviderFactory {

	// URI Matcher information
	public static final String		URI_BASE			= "com.novoda.runbuddy";
	public static final UriMatcher	URI_MATCHER			= new UriMatcher(UriMatcher.NO_MATCH);

	public static final int			ROUTES				= 0;
	public static final int			ROUTE_ID			= 1;
	public static final int			ROUTE_POINTS		= 2;
	public static final int			ROUTE_POINTS_ID		= 3;

	public static final int			CHECKPOINTS			= 4;
	public static final int			CHECKPOINT_ID		= 5;

	public static final int			ROUTE_SEGMENTS		= 6;
	public static final int			ROUTE_SEGMENT_ID	= 7;

	public static final int			RUNS				= 8;
	public static final int			RUN_ID				= 9;
	public static final int			RUN_SEGMENTS		= 10;
	public static final int			RUN_SEGMENT_ID		= 11;

	static {
		URI_MATCHER.addURI(URI_BASE, "routes", ROUTES);
		URI_MATCHER.addURI(URI_BASE, "routes/#", ROUTE_ID);
		URI_MATCHER.addURI(URI_BASE, "routes/#/points", ROUTE_POINTS);
		URI_MATCHER.addURI(URI_BASE, "routes/#/points/#", ROUTE_POINTS_ID);

		URI_MATCHER.addURI(URI_BASE, "runs", RUNS);

		// Checkpoints are idependent entities. They are usually created by
		// known places or by time/position against a route. But in essence they
		// are idependant. If you look in the db, they only have one foreign key
		// against a geolocation
		URI_MATCHER.addURI(URI_BASE, "checkpoints/", CHECKPOINTS);
		URI_MATCHER.addURI(URI_BASE, "checkpoints/#", CHECKPOINT_ID);

		// Route Segments - this links the route to a run via checkpoints
		URI_MATCHER.addURI(URI_BASE, "routes/#/segments", ROUTE_SEGMENTS);
		URI_MATCHER.addURI(URI_BASE, "routes/#/segments/#", ROUTE_SEGMENT_ID);

		// Run Provider Uris
		URI_MATCHER.addURI(URI_BASE, "routes/#/runs", RUNS);
		URI_MATCHER.addURI(URI_BASE, "routes/#/runs/#", RUN_ID);
		URI_MATCHER.addURI(URI_BASE, "routes/#/runs/#/segments", RUN_SEGMENTS);
		URI_MATCHER.addURI(URI_BASE, "routes/#/runs/#/segments/#", RUN_SEGMENT_ID);
	}

	public IRBContentProvider getContentProvider(Uri uri, ContentProvider cp, SQLiteOpenHelper mOpenHelper) {
		int match = URI_MATCHER.match(uri);
		switch (match) {
			case ROUTES:
				return new RoutesProvider(cp, mOpenHelper);
			case ROUTE_ID:
				return new RoutesProvider(cp, mOpenHelper);
			case ROUTE_POINTS:
				return new RoutesProvider(cp, mOpenHelper);
			case ROUTE_SEGMENTS:
				return new RoutesProvider(cp, mOpenHelper);
			case ROUTE_SEGMENT_ID:
				return new RoutesProvider(cp, mOpenHelper);
			case CHECKPOINTS:
				return new CheckPointsProvider(cp, mOpenHelper);
			case CHECKPOINT_ID:
				return new CheckPointsProvider(cp, mOpenHelper);
			case RUNS:
				return new RunsProvider(cp, mOpenHelper);
			case RUN_ID:
				return new RunsProvider(cp, mOpenHelper);
			case RUN_SEGMENTS:
				return new RunsProvider(cp, mOpenHelper);
			default:
				throw new IllegalArgumentException("Unknown URL " + uri);
		}
	}
}
